package com.ktjy.web;

import com.ktjy.entity.Auctionuser;
import com.ktjy.service.AuctionUserService;
import com.ktjy.service.impl.AuctionUserServiceImpl;
import my.framework.anno.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: DABINGCHNEGJIAO
 * @CreateTime: 2025-06-14
 * @Description:
 * @Version: 1.0
 */
public class AuctionUserControll {
    AuctionUserService auctionUserService = new AuctionUserServiceImpl();

    @RequestMapping("/register")
    public String registerAuctionuser(HttpServletRequest request, HttpServletResponse response) {
        //   获取表单数据
        String userIsAdmin = request.getParameter("userIsAdmin");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String idCard = request.getParameter("idCard");
        String phone = request.getParameter("phone");
        String address = request.getParameter("address");
        String postcode = request.getParameter("postcode");
        Auctionuser auctionuser = new Auctionuser();
        auctionuser.setUserName(username);
        auctionuser.setUserPassword(password);
        auctionuser.setUserCardNo(idCard);
        auctionuser.setUserTel(phone);
        auctionuser.setUserAddress(address);
        auctionuser.setUserPostNumber(postcode);
        auctionuser.setUserIsAdmin(userIsAdmin.equals("1") ? true : false);
        boolean b = auctionUserService.registerAuctionuser(auctionuser);
        if (b) {
            if (userIsAdmin.equals("1")) {
                //注册成功，跳转到管理员登录页面
                return "redirect:/login-admin.jsp";
            } else {
                return "redirect:/login.jsp";
            }
        } else {
            //注册失败，跳转到注册页面
            return "redirect:/register.jsp?userIsAdmin=" + userIsAdmin;
        }
    }

    @RequestMapping("/login")
    public String loginAuctionuser(HttpServletRequest request, HttpServletResponse response) {
        //   获取表单数据
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String userIsAdmin = request.getParameter("userIsAdmin");
        Auctionuser auctionuser = auctionUserService.loginAuctionuser(username, password);

        if (auctionuser != null) {
            if ((auctionuser.isUserIsAdmin() ? "1" : "0").equals(userIsAdmin)) {
                request.getSession().setAttribute("userIsAdmin", auctionuser.isUserIsAdmin() ? "1" : "0");
                request.getSession().setAttribute("auctionuser", auctionuser);
                //登录成功，跳转到拍品查询页面
                return "redirect:/do/page?userIsAdmin=" + userIsAdmin;
            }
        }
        return userIsAdmin.equals("1") ? "redirect:/login-admin.jsp" : "redirect:/login.jsp";
    }
}
